package com.carl.leetcode.pointer;

import java.util.Arrays;

/**
 * Created by carlXie on 2016/3/16.
 */
public class RemoveDuplicates {

    public static int removeDuplicates(int[] nums) {
        if(nums == null) return 0;
        if(nums.length == 1) return 1;

        int curIndex = 0;
        int nextIndex = 1;
        boolean duplicateFlag = false;

        while(nextIndex < nums.length) {
            if(nums[curIndex] != nums[nextIndex]) {
                if(duplicateFlag) {
                    nums[++curIndex] = nums[nextIndex];
                    duplicateFlag = false;
                } else {
                    ++curIndex;
                    ++nextIndex;
                }
            } else {
                ++nextIndex;
                duplicateFlag = true;
            }
        }

        return curIndex + 1;
    }

    public static void main(String[] args) {
        int[] nums = {1, 1, 2, 3};

        System.out.println(removeDuplicates(nums));
    }
}
